<?php

include '../../db.php';
$connect = pg_connect($connectionString);
if (!$connect) {
    echo "connect : A connection to the server could not be established!";
    return false;
}
$params = array();
array_push($params, pg_escape_string($_POST["id"]));
array_push($params, pg_escape_string($_POST["name"]));
array_push($params, pg_escape_string($_POST["code"]));
array_push($params, pg_escape_string($_POST["type"]));
//array_push($params, pg_escape_string($_POST["cost"]));
//array_push($params, pg_escape_string($_POST["price"]));
//array_push($params, pg_escape_string($_POST["quantity"]));
//array_push($params, pg_escape_string($_POST["fullname"]));
array_push($params, pg_escape_string($_POST["category_id"]));
array_push($params, pg_escape_string($_POST["subcategory_id"]));
array_push($params, pg_escape_string($_POST["brand_id"]));
array_push($params, pg_escape_string($_POST["model_id"]));
array_push($params, pg_escape_string($_POST["scaffold_id"]));
array_push($params, pg_escape_string($_POST["file_id"]));
array_push($params, pg_escape_string($_POST["position_id"]));
//array_push($params, pg_escape_string($_POST["unit_id"]));

$full_name = "";
$location = "";
$str_category = "";


//----------------------------------------------------------------------
$result = pg_query($connect, "SELECT id, name FROM warehouse_category WHERE id = '" . $_POST["category_id"] . "'");
$arrRegistros = pg_fetch_all($result);
$str_category .= $arrRegistros[0]['name'];
pg_free_result($result);
//----------------------------------------------------------------------
$result = pg_query($connect, "SELECT id, name FROM warehouse_subcategory WHERE id = '" . $_POST["subcategory_id"] . "'");
$arrRegistros = pg_fetch_all($result);
if ($arrRegistros[0]['name'] != "") {
    $str_category .= " - " . $arrRegistros[0]['name'];
}
pg_free_result($result);
//----------------------------------------------------------------------
$result = pg_query($connect, "SELECT id, name FROM warehouse_brand WHERE id = '" . $_POST["brand_id"] . "'");
$arrRegistros = pg_fetch_all($result);
$full_name .= $arrRegistros[0]['name'] . " ";
pg_free_result($result);
//----------------------------------------------------------------------
$result = pg_query($connect, "SELECT id, name FROM warehouse_model WHERE id = '" . $_POST["model_id"] . "'");
$arrRegistros = pg_fetch_all($result);
$full_name .= $arrRegistros[0]['name'] . " ";
pg_free_result($result);
//----------------------------------------------------------------------
$result = pg_query($connect, "SELECT id, name FROM warehouse_scaffold WHERE id = '" . $_POST["scaffold_id"] . "'");
$arrRegistros = pg_fetch_all($result);
$location .= "Andamio :" . $arrRegistros[0]['name'] . " ";
pg_free_result($result);
//----------------------------------------------------------------------
$result = pg_query($connect, "SELECT id, name FROM warehouse_file WHERE id = '" . $_POST["file_id"] . "'");
$arrRegistros = pg_fetch_all($result);
$location .= "Fila :" . $arrRegistros[0]['name'] . " ";
pg_free_result($result);
//----------------------------------------------------------------------
$result = pg_query($connect, "SELECT id, name FROM warehouse_position WHERE id = '" . $_POST["position_id"] . "'");
$arrRegistros = pg_fetch_all($result);
$location .= "Posicion :" . $arrRegistros[0]['name'] . " ";
pg_free_result($result);
//----------------------------------------------------------------------

$full_name .= $_POST["type"] . " " . $_POST["name"];
//------------------------------------------------------------------------------
array_push($params, pg_escape_string($full_name));
array_push($params, pg_escape_string($location));
array_push($params, pg_escape_string($str_category));

$stmt = pg_prepare($connect, "update_product", "UPDATE warehouse_product SET "
        . "name=$2,code=$3,type=$4"
        //. ",cost=$5,price=$6,"
        //. ",quantity=$5"
        //. "fullname=$8,"
        . ",category_id=$5"
        . ",subcategory_id=$6"
        . ",brand_id=$7"
        . ",model_id=$8"
        . ",scaffold_id=$9"
        . ",file_id=$10"
        . ",position_id=$11"
        . ",fullname=$12"
        . ",location=$13"
        . ",str_category=$14"
        //. ",unit_id=$16"
        . " WHERE id = $1 ");

$result = pg_execute($connect, "update_product", $params);

if (!$result) {

    die("Error in SQL query: " . pg_last_error());
}

echo "Data successfully inserted!";

pg_free_result($result);
pg_close($connect);
